package com.haizhi.crm.biz.builder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.haizhi.crm.biz.constant.MessageConstant;
import com.haizhi.crm.biz.model.MessageSourceDo;
import com.haizhi.crm.common.util.DateUtils;
import com.haizhi.crm.search.model.SearchQo;

import java.util.*;

/**
 * Created by wangshan on 2018/5/23.
 */
public class MsgSearchQoBuilder {

    public static SearchQo get(MessageSourceDo messageSourceDo) {
        SearchQo searchQo = new SearchQo();
        searchQo.setGraph(messageSourceDo.getDatabase());
        searchQo.setDomain(messageSourceDo.getDatabase());
        Set schems = new HashSet();
        schems.add(messageSourceDo.getSchema());
        searchQo.setSchemas(schems);
        String rule = messageSourceDo.getRule();
        //get filter rule
        Date currentTime = new Date();
        Map<String, String> rules = JSON.parseObject(rule, new TypeReference<Map<String, String>>() {
        });
        String fieldName = rules.get(MessageConstant.FIELD);

        Date lastPullTime = messageSourceDo.getLastPullTime();
        String fromDate = DateUtils.formatLocal(lastPullTime);
        String toDate = DateUtils.formatLocal(currentTime);

        //constructor searchQo
        Map ranges = new HashMap();
        ranges.put(MessageConstant.FROM, fromDate);
        ranges.put(MessageConstant.TO, toDate);
        Map<String, Object> map = new HashMap<>();
        map.put(MessageConstant.TYPE, MessageConstant.RANGE);
        map.put(MessageConstant.FIELD, fieldName);
        List range = new ArrayList();
        range.add(ranges);
        map.put(MessageConstant.RANGES, range);
        List filter = new ArrayList();
        filter.add(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put(MessageConstant.TYPE, MessageConstant.TERM);
        map1.put(MessageConstant.FIELD, "msg_generated" );
        map1.put(MessageConstant.VALUES, Arrays.asList("false" ));

        filter.add(map1);

        searchQo.setFilter(filter);
        return searchQo;
    }

    /*
      "filter": [
    {
      "field": "province",
      "type": "term",
      "values": ["广东"]
    },
     */
    public static SearchQo get(String database, String companyId) {
        SearchQo searchQo = new SearchQo();
        searchQo.setGraph(database);
        searchQo.setDomain(database);
        Set schems = new HashSet();
        schems.add(MessageConstant.COMPANY);
        searchQo.setSchemas(schems);
        List filter = new ArrayList();
        Map<String, Object> map = new HashMap<>();
        map.put(MessageConstant.TYPE, MessageConstant.TERM);
        map.put(MessageConstant.FIELD, MessageConstant.OBJECT_KEY);
        List values = new ArrayList();
        values.add(companyId);
        map.put(MessageConstant.VALUES, values);
        filter.add(map);
        searchQo.setFilter(filter);
        return searchQo;
    }

    public static SearchQo get(String database, String schemas, String key) {
        SearchQo searchQo = new SearchQo();
        searchQo.setGraph(database);
        searchQo.setDomain(database);
        Set schems = new HashSet();
        schems.add(schemas);
        searchQo.setSchemas(schems);
        List filter = new ArrayList();
        Map<String, Object> map = new HashMap<>();
        map.put(MessageConstant.TYPE, MessageConstant.TERM);
        map.put(MessageConstant.FIELD, MessageConstant.OBJECT_KEY);
        List values = new ArrayList();
        values.add(key);
        map.put(MessageConstant.VALUES, values);
        filter.add(map);
        searchQo.setFilter(filter);
        return searchQo;
    }

    public static SearchQo get(String database, String schemas, String field, List<String> keys) {
        SearchQo searchQo = new SearchQo();
        searchQo.setGraph(database);
        searchQo.setDomain(database);
        Set schems = new HashSet();
        schems.add(schemas);
        searchQo.setSchemas(schems);
        List filter = new ArrayList();
        Map<String, Object> map = new HashMap<>();
        map.put(MessageConstant.TYPE, MessageConstant.TERM);
        map.put(MessageConstant.FIELD, field);
        List values = new ArrayList();
        for (String key : keys) {
            values.add(key);
        }
        map.put(MessageConstant.VALUES, values);
        filter.add(map);
        searchQo.setFilter(filter);
        return searchQo;
    }

}
